package com.example.tao.mycar.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.DeleteCallback;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.example.tao.mycar.MyActionBarActivity;
import com.example.tao.mycar.MyApplication;
import com.example.tao.mycar.R;
import com.example.tao.mycar.database.OperateDBUtils;
//import com.example.tao.mycar.net.NetWorkRequest;
import com.example.tao.mycar.view.datepicker.DatePickerDialog;

import java.util.List;

public class InputWeightActivity extends MyActionBarActivity implements View.OnClickListener {
    EditText inputWeight;
    TextView inputWeightDate;
    Button saveWeightBtn;
    private DatePickerDialog mChangeDateDialog;
    //private NetWorkRequest netWorkRequest;
    private OperateDBUtils operateDBUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_input_weight);
        setToolBar(getResources().getString(R.string.title_activity_input_weight), true);

        inputWeight = (EditText) findViewById(R.id.input_weight);
        inputWeightDate = (TextView) findViewById(R.id.txt_weight_date_input);
        saveWeightBtn = (Button) findViewById(R.id.btn_save);
        inputWeight.addTextChangedListener(textWatcher);
        inputWeightDate.setOnClickListener(this);
        saveWeightBtn.setOnClickListener(this);

        //netWorkRequest = new NetWorkRequest(this);
        operateDBUtils = new OperateDBUtils(this);
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (!TextUtils.isEmpty(charSequence)) {
                inputWeight.setCursorVisible(true);
            } else {
                inputWeight.setCursorVisible(false);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    };

    @Override
    public void onClick(View view) {
        if (view == inputWeightDate) {
            if (mChangeDateDialog != null && mChangeDateDialog.isShowing()) {
                return;
            }
            mChangeDateDialog = new DatePickerDialog(
                    InputWeightActivity.this);
            DatePickerDialog.setWindowSize(mChangeDateDialog);
            mChangeDateDialog.show();
            mChangeDateDialog.setBirthdayListener(new DatePickerDialog.OnBirthListener() {
                @Override
                public void onClick(String year, String month, String day) {
                    inputWeightDate.setText(year + "-" + month + "-" + day);
                }
            });
        }
        /*
        if (view == saveWeightBtn) {
            final String weight = inputWeight.getText().toString();
            final String time = inputWeightDate.getText().toString();
            if (!netWorkRequest.isNetworkConnected()) {
                Toast.makeText(this, "保持网络连接才能保存数据", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!TextUtils.isEmpty(weight) && !TextUtils.isEmpty(time)) {
                netWorkRequest.getProgressDialog().show();
                netWorkRequest.isExistTheTimeOnTable(NetWorkRequest.BABY_WEIGHT, time, new FindCallback<AVObject>() {
                    @Override
                    public void done(List<AVObject> list, AVException e) {
                        if (e == null) {
                            if (list.size() == 0) {
                                Log.d("ZZ", "Not exist the time`s weight, add to server");
                                addWeightToServer(weight, time);
                            } else if (list.size() == 1) {
                                Log.d("ZZ", "Exist the time`s weight, update to server");
                                updateWeightToServer(weight, time);
                            } else if (list.size() > 1) {
                                AVObject.deleteAllInBackground(list, new DeleteCallback() {
                                    @Override
                                    public void done(AVException e) {
                                        if (e == null) {
                                            Log.d("ZZ", "Exist the time, But > 1 So deleteAll success");
                                            addWeightToServer(weight, time);
                                        } else {
                                            Toast.makeText(InputWeightActivity.this, "上传宝贝体重失败", Toast.LENGTH_SHORT).show();
                                            Log.d("ZZ", "Exist the time, But > 1 So deleteAll fail");
                                        }
                                    }
                                });
                            }
                        } else {
                            Toast.makeText(InputWeightActivity.this, "上传宝贝体重失败", Toast.LENGTH_SHORT).show();
                            Log.d("ZZ", "isSaveBabyWeightOnTheTime e = " + e);
                        }
                    }
                });
            } else {
                Toast.makeText(this, "请填写时间和体重", Toast.LENGTH_SHORT).show();
            }
        } */
    }
    /*
    private void addWeightToServer(final String weight, final String time) {
        Log.d("ZZ", "add data");
        netWorkRequest.addWeightAndTimeToServer(weight, time, new SaveCallback() {
            @Override
            public void done(AVException e) {
                netWorkRequest.getProgressDialog().dismiss();
                if (e == null) {
                    finish();
                    operateDBUtils.saveWeightToDB(weight, time);
                } else {
                    Toast.makeText(InputWeightActivity.this, "上传宝贝体重失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void updateWeightToServer(final String weight, final String time) {
        Log.d("ZZ", "update data");
        netWorkRequest.updateWeightAndTimeToServer(weight, time, new SaveCallback() {
            @Override
            public void done(AVException e) {
                netWorkRequest.getProgressDialog().dismiss();
                if (e == null) {
                    finish();
                    operateDBUtils.saveWeightToDB(weight, time);
                } else {
                    Toast.makeText(InputWeightActivity.this, "上传宝贝体重失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    */
}
